package com.ruoyi.project.manage.invest.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.manage.approveLog.domain.ApproveLog;
import com.ruoyi.project.manage.approveLog.service.IApproveLogService;
import com.ruoyi.project.manage.detail.domain.ProjectInvestDetail;
import com.ruoyi.project.manage.detail.service.IProjectInvestDetailService;
import com.ruoyi.project.manage.project.domain.Project;
import com.ruoyi.project.manage.project.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.manage.invest.domain.ProjectInvest;
import com.ruoyi.project.manage.invest.service.IProjectInvestService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 资金落实记录Controller
 * 
 * @author fangkun
 * @date 2022-03-28
 */
@Controller
@RequestMapping("/manage/invest")
public class ProjectInvestController extends BaseController
{
    private String prefix = "manage/invest";

    @Autowired
    private IProjectInvestService projectInvestService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IApproveLogService approveLogService;
    @Autowired
    private IProjectInvestDetailService investDetailService;

    @RequiresPermissions("manage:invest:view")
    @GetMapping()
    public String invest()
    {
        return prefix + "/invest";
    }

    /**
     * 查询资金落实记录列表
     */
    @RequiresPermissions("manage:invest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project)
    {
        startPage();
        project.setProjectStatus(ProjectConstants.APPROVAL_AGREE);
        // 设置当前登录人查看本人录入的项目信息
        project.setCreateBy(ShiroUtils.getLoginName());
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出资金落实记录列表
     */
    @RequiresPermissions("manage:invest:export")
    @Log(title = "资金落实记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectInvest projectInvest)
    {
        List<ProjectInvest> list = projectInvestService.selectProjectInvestList(projectInvest);
        ExcelUtil<ProjectInvest> util = new ExcelUtil<ProjectInvest>(ProjectInvest.class);
        return util.exportExcel(list, "资金落实记录数据");
    }

    /**
     * 新增资金落实记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资金落实记录
     */
    @RequiresPermissions("manage:invest:add")
    @Log(title = "资金落实记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectInvest projectInvest)
    {
        return toAjax(projectInvestService.insertProjectInvest(projectInvest));
    }

    /**
     * 修改资金落实记录
     */
    @RequiresPermissions("manage:invest:edit")
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        // 查询项目信息
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);
        // 获取当前年
        String currentYear = DateUtils.dateTimeNow("yyyy");
        mmap.put("currentYear", currentYear);
        // 项目投资情况统计
        List<Map> investStat = investDetailService.statProjectInvest(projectId);
        if (investStat.size() == 0) {
            Map defaultMap = new HashMap<>();
            defaultMap.put("year", currentYear);
            defaultMap.put("doneInvest", 0);
            investStat.add(defaultMap);
        }
        mmap.put("investStat", investStat);
        // 项目投资明细
        ProjectInvestDetail detailParam = new ProjectInvestDetail();
        detailParam.setProjectId(projectId);
        detailParam.setYear(Integer.valueOf(currentYear));
        List<ProjectInvestDetail> projectInvestDetails = investDetailService.selectProjectInvestDetailList(detailParam);

        if (projectInvestDetails == null || projectInvestDetails.size() == 0) {
            projectInvestDetails = new ArrayList<>();
            for (int i = 1; i <= 12; i++) {
                ProjectInvestDetail detail = new ProjectInvestDetail();
                detail.setYear(Integer.valueOf(currentYear));
                detail.setMonth(i);
                detail.setProjectId(projectId);
                projectInvestDetails.add(detail);
            }
        }

        mmap.put("projectInvestDetails", projectInvestDetails);
        // 查询资金落实记录
        ProjectInvest projectInvest = new ProjectInvest();
        projectInvest.setProjectId(projectId);
        projectInvest.setStatus(UserConstants.NORMAL);
        projectInvest.setInvestStatus(ProjectConstants.APPROVAL_AGREE);
        List<ProjectInvest> projectInvests = projectInvestService.selectProjectInvestList(projectInvest);
        mmap.put("projectInvests", projectInvests);

        // 查询审批流转记录
        // 获取流转信息
        ApproveLog param = new ApproveLog();
        param.setEntityId(projectId);
        param.setEntityType(ProjectConstants.APPROVAL_TYPE_INVEST);
        List<ApproveLog> approveLogs = approveLogService.selectApproveLogList(param);
        mmap.put("approveLogs", approveLogs);

        return prefix + "/edit";
    }

    @RequiresPermissions("manage:invest:edit")
    @GetMapping("/detail/{projectId}")
    public String detail(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        // 查询项目信息
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);
        // 获取当前年
        String currentYear = DateUtils.dateTimeNow("yyyy");
        mmap.put("currentYear", currentYear);
        // 项目投资情况统计
        List<Map> investStat = investDetailService.statProjectInvest(projectId);
        if (investStat.size() == 0) {
            Map defaultMap = new HashMap<>();
            defaultMap.put("year", currentYear);
            defaultMap.put("doneInvest", 0);
            investStat.add(defaultMap);
        }
        mmap.put("investStat", investStat);
        // 项目投资明细
        ProjectInvestDetail detailParam = new ProjectInvestDetail();
        detailParam.setProjectId(projectId);
        detailParam.setYear(Integer.valueOf(currentYear));
        List<ProjectInvestDetail> projectInvestDetails = investDetailService.selectProjectInvestDetailList(detailParam);

        if (projectInvestDetails == null || projectInvestDetails.size() == 0) {
            projectInvestDetails = new ArrayList<>();
            for (int i = 1; i <= 12; i++) {
                ProjectInvestDetail detail = new ProjectInvestDetail();
                detail.setYear(Integer.valueOf(currentYear));
                detail.setMonth(i);
                detail.setProjectId(projectId);
                projectInvestDetails.add(detail);
            }
        }

        mmap.put("projectInvestDetails", projectInvestDetails);
        // 查询资金落实记录
        ProjectInvest projectInvest = new ProjectInvest();
        projectInvest.setProjectId(projectId);
        projectInvest.setStatus(UserConstants.NORMAL);
        projectInvest.setInvestStatus(ProjectConstants.APPROVAL_AGREE);
        List<ProjectInvest> projectInvests = projectInvestService.selectProjectInvestList(projectInvest);
        mmap.put("projectInvests", projectInvests);

        // 查询审批流转记录
        // 获取流转信息
        ApproveLog param = new ApproveLog();
        param.setEntityId(projectId);
        param.setEntityType(ProjectConstants.APPROVAL_TYPE_INVEST);
        List<ApproveLog> approveLogs = approveLogService.selectApproveLogList(param);
        mmap.put("approveLogs", approveLogs);

        return prefix + "/detail";
    }

    /**
     * 修改保存资金落实记录
     */
    @RequiresPermissions("manage:invest:edit")
    @Log(title = "资金落实记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectInvest projectInvest)
    {
        return toAjax(projectInvestService.updateProjectInvest(projectInvest));
    }

    /**
     * 提交项目信息
     */
    @RequiresPermissions("manage:invest:edit")
    @Log(title = "资金落实记录", businessType = BusinessType.UPDATE)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(int projectId)
    {
        return toAjax(projectInvestService.submit(projectId));
    }

    /**
     * 撤回项目
     */
    @RequiresPermissions("manage:invest:edit")
    @Log(title = "资金落实记录", businessType = BusinessType.UPDATE)
    @PostMapping("/withdraw")
    @ResponseBody
    public AjaxResult withdraw(int projectId)
    {
        return toAjax(projectInvestService.withdraw(projectId));
    }

    /**
     * 删除资金落实记录
     */
    @RequiresPermissions("manage:invest:remove")
    @Log(title = "资金落实记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectInvestService.deleteProjectInvestByProjectInvestIds(ids));
    }
}
